<template>
  <div class="login">
    <div class="container">
      <div class="avatar">
        <img src="../assets/p.png" alt="" />
      </div>
      <el-input
        placeholder="请输入账号"
        prefix-icon="el-icon-user-solid"
        v-model="user.username"
      >
      </el-input>
      <el-input
        placeholder="请输入密码"
        prefix-icon="el-icon-key"
        v-model="user.password"
        show-password
      >
      </el-input>
      <el-button
        @click="loginFn"
        :plain="true"
        class="login-btn"
        type="primary"
        round
        >主要按钮</el-button
      >
    </div>
  </div>
</template>

<script>
import { login } from "@/API/user";
export default {
  data() {
    return {
      user: { username: "17673849042", password: "" },
    };
  },
  methods: {
    loginFn() {
      let numReg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      let pwdReg = /^.{3,10}$/;
      if (
        !numReg.test(this.user.username) ||
        !pwdReg.test(this.user.password)
      ) {
        this.$message({
          message: "输入不合法！！！",
          type: "warning",
        });
        return;
      }
      login({
        username: this.user.username,
        password: this.user.password,
      }).then((res) => {
        // console.log(res);
        if (res.data.message == "登录成功") {
          localStorage.setItem(
            "hm-back-token",
            JSON.stringify(res.data.data.token)
          );
          this.$router.push({
            path: "/index",
          });
        }
        // 提示登录状态
        this.$message({
          message: res.data.message,
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
      img {
        width: 100%;
      }
    }
    .el-input {
      margin: 10px 0;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>